# coding: utf8


YD = ['135','136','137','138','139','147','148','150','151','152','157','158','159','182','183','184','187','188','178','198']
LT = ['130','131','132','140','145','146','155','156','166','185','186','175','176']
DX = ['133','149','153','180','181','189','177','173','174','149','199']


def check_operator(telephone):
	if telephone[:3] in YD:
		return 1
	elif telephone[:3] in LT:
		return 2
	elif telephone[:3] in DX:
		return 3
	elif telephone[:4] == '1349':
		return 3
	elif telephone[:3] == '134':
		return 1
	else:
		return 0


def re_telephone(string):
	data = re.findall(r'\d{11}', string)
	if data:
		return data[0]


def csv_2_data(fn):
	datas = set()
	csv_reader = csv.reader(open(fn))
	for row in csv_reader:
		for cloumn in row:
			telephone = re_telephone(cloumn)
			if telephone:
				datas.add(telephone)
	return list(set(datas))


def txt_2_data(fn):
	datas = set()
	with open(fn) as f:
		for line in f.readlines():
			telephone = re_telephone(line)
			if telephone:
				datas.add(telephone)
	return list(set(datas))


def xlsx_22_data(fn):
	datas = set()
	items = xlsx_2_data(fn)
	for rows in items:
		for row in rows:
			for cloumn in row:
				telephone = re_telephone(convert_2_str(cloumn))
				if telephone:
					datas.add(telephone)
	return list(set(datas))


filenames = []
def get_filenames(currentPath):
	global filenames
	if not os.path.exists(currentPath):
		return filenames
	if os.path.isfile(currentPath):
		if currentPath.endswith('.csv') or currentPath.endswith('.xls') or currentPath.endswith('.xlsx') or currentPath.endswith('.txt'):
			filenames.append(currentPath)
	elif os.path.isdir(currentPath):
		pathList = os.listdir(currentPath)
		for folder_name in pathList:
			if '.git' in folder_name:
				continue
			get_filenames(currentPath + '\\' + folder_name)


def excel_2_txt(fn, num):
	datas = xlsx_2_data(fn)
	data = []
	for rows in datas:
		for row in rows:
			data.append(str(row[0]))
	with open('data\\33\\%s.txt' % num, 'a') as f:
		f.write('\n'.join(data)) 

def clean_bad():
	with open('bad.txt', 'w') as f:
		f.write('')
	logging.info(sys_encode("清空 bad.txt 文件"))

def check_bad():
	if os.path.getsize('bad.txt'):
		fs = []
		with open('bad.txt') as f:
			fs = f.readlines()
		logging.info(sys_encode("\n解析失败的文件有: \n%s " % ''.join(fs)))
def write_bad(f):
	with open('bad.txt', 'a') as fp:
		fp.write(f + '\n')


def file_max_num(filenames):
	nums = [0]
	for f in filenames:
		string = os.path.splitext(f)[0]
		if string.isdigit(): # 判断是否是数字
			nums.append(int(string))
	return max(nums)
  
def filenames(file_dir):   
    return os.listdir(file_dir)
    

def load_execl(datas, num):
	fname = 'end_data\\11\\%s.xls' % num
	rows = []
	row1 = [datas[0]]
	for data in datas[1:]:
		rows.append([data])
	try:
		data_2_xlsx(fname, row1, rows)
		logging.info(sys_encode("导出数据成功, 文件名: %s" % fname))
	except Exception as e:
		logging.info(sys_encode(e))

def load_txt(datas, num):
	num += 1
	try:
		fname = 'end_data\\%s.txt' % num
		with open(fname, 'w') as f:
			f.write('\n'.join(datas))
		logging.info(sys_encode("导出数据成功, 文件名: %s" % fname))
	except Exception as e:
		logging.info(sys_encode(e))